package com.example.mybatis_demo.controller;

import com.example.mybatis_demo.model.ArticleInfo;
import com.example.mybatis_demo.model.User;
import com.example.mybatis_demo.service.UserService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: fzykd
 * Date: 2022-12-19
 * Time: 13:20
 */
@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    //为什么注入了这个实体对象无法启动
//    @Autowired
//    private User user;

    @RequestMapping("/getId")
    private User getUserById(Integer id){
        return userService.getUserById(id);
    }

    @RequestMapping("/updateId")
    private int updateId(@Param("id") Integer id, @Param("name") String name){
        return userService.updateById(id,name);
    }

    //普通的新增方法 这个添加之后user的id是null
    @RequestMapping("/add")
    private int add(){
        User user1 = new User();
        user1.setName("李四");
        user1.setPassword("lqh_qwe");
        user1.setPhoto("D:/123123");
        int ret = userService.add(user1);
        System.out.println(user1.getId());
        return ret;
    }

    //返回收影响的行数 和 自增的id 设置了属性 可以获取到添加之后的id
    @RequestMapping("/add1")
    private int add1(){
        User user2 = new User();
        user2.setName("小红");
        user2.setPassword("123qwe456");
        user2.setPhoto("E:/date");
        int ret = userService.add1(user2);
        System.out.println(user2.getId());
        return ret;
    }

    @RequestMapping("/del")
    private int del(@Param("id") Integer id){
        return userService.del(id);
    }


    @RequestMapping("/order")
    private List<User> getOrder(@Param("order") String order){
        return userService.getOrder(order);
    }

    @RequestMapping("/like")
    private List<User> getLike(@Param("username") String username){
        return userService.getLike(username);
    }

    @RequestMapping("/map")
    private User getIdMap(@Param("id")Integer id){
        return userService.getIdMap(id);
    }

    //动态SQL  if标签 做判断
    @RequestMapping("/add2")
    private int add2(){
        User user = new User();
        user.setName("小张");
        user.setPassword("123890");
        //判断空的依据是 直接不写 而不是传了是空的
        user.setPhoto("");
        return userService.add2(user);
    }

    //trim标签 主要作用就是去除SQL语句前后多余某个字符
}
